$(document).ready(function(){
    $('.btn').click(function(){ra.btnClick(this);});
    $('input[name=pick_method]').change(function(){ra.radioChange(this);});
    $('#pick_page_method').change(function(){ra.pageMethodChange(this);});
    $('#s_ablum').change(function(){ra.ablumeChange();});
});

var ra = {};
(function(r){
    r.rulesindex = 1;
    r.btnClick = function(target){
        if($(target).is("label") == false){
           var id = $(target).attr("id");
           var html = "";
           switch(id){
                case 'add_rule':
                    r.rulesindex++;
                    html = '<li>'+
            '<div>规则'+r.rulesindex+'[<label class=\'btn\'>删除</label>]:</div>'+
            '<div>'+
                  '<span>名称:<input type="text" value="" name="pick_rule_name[]"  class="input"></span>'+
                  '<span>来源:<input type="text" value="" name="pick_source_name[]"  class="input"></span>'+
                  '<span>内容:<textarea name="pick_rule_contents[]"></textarea></span>'+
            '</div>'+
        '</li>';
                    var li = $(html).insertBefore($(target).parent());
                    var btn = $($(li).children('div')[0]).children('.btn');
                    $(btn[0]).bind('click', function(){ra.btnClick(btn[0]);});
                break;
                /*case 'add_save':
                    html = ' <span>规则内容名:<input type="text" value="" name="pick_save_rule_name[]"  class="input">'+
                  ' 保存数据名:<input type="text" value="" name="pick_save_data_name[]"  class="input">[<label class=\'btn\'>删除</label>]</span>';
                    var span = $(html).insertBefore($(target));
                    var btn = $(span).children('.btn');
                    $(btn[0]).bind('click', function(){ra.btnClick(btn[0]);});
                break;*/
                default:
                    return ;
           }
        }
        else{
            var o = $(target).parent();
            if($(o).is("span")){
                $(o).remove();
            }
            else{
                $(o).parent().remove();
            }
        }
    };

    r.ablumeChange = function(){
         var ab = $('#s_ablum').val();
         console.log(ab);
         if(ab!=0){
            $('#ablum').val($('#s_ablum').find("option:selected").text().replace(/\([0-9]*\)/, ''));
         }
         else{
            $('#ablum').val('');
         }
    };

    r.radioChange = function(target){
        var rval = $(target).val();
        switch(parseInt(rval)){
            case 0:
                $(target).parent().after('<div class="pick_method_time">采集时间:<input type="text" value="" name="pick_method_time"></div>');
            break;
            case 1:
                if($(".pick_method_time")){
                    $(".pick_method_time").remove();
                }
            break;
            default:
                return ;
        };
    };

    r.pageMethodChange = function(target){
        var mval = $(target).val();
        switch(parseInt(mval)){
            case 2:
               var o = $("<label class='btn'>新增网址</label>").appendTo($('.urls'));
               $(o).bind('click', function(){
                    var url = $('<span class="urls"><input type="text" value="" name="pick_page_url[]" class="url_input"><label class=\'btn\'>[删除]</label></span>').appendTo($('#pick_page_url'));
                    $(url).children('.btn').bind('click', function(){
                        $(url).remove();
                    });
               });
            break;
            case 1:
                var i = 0;
                $("span[class='urls']").each(function(){
                    i++;
                    if(i==1){
                        $(this).children('.btn').remove();
                    }
                    else{
                        $(this).remove();
                    }
                });
            break;
            default:
                return ;
        }
    };
})(ra);